package net.maku.im.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.im.dto.ImSensitiveWordEnabledDTO;
import net.maku.im.service.ImSensitiveWordService;
import net.maku.im.dto.ImSensitiveWordQueryDTO;
import net.maku.im.vo.ImSensitiveWordVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 敏感词
*
* @author Blue 
* @since 1.0.0 2024-07-20
*/
@RestController
@RequestMapping("im/sensitive/word")
@Tag(name="敏感词")
@AllArgsConstructor
public class ImSensitiveWordController {
    private final ImSensitiveWordService sensitiveWordService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('im:sensitive:page')")
    public Result<PageResult<ImSensitiveWordVO>> page(@ParameterObject @Valid ImSensitiveWordQueryDTO query){
        PageResult<ImSensitiveWordVO> page = sensitiveWordService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('im:sensitive:info')")
    public Result<ImSensitiveWordVO> get(@PathVariable("id") Long id){
        ImSensitiveWordVO vo = sensitiveWordService.info(id);
        return Result.ok(vo);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('im:sensitive:save')")
    public Result<String> save(@RequestBody ImSensitiveWordVO vo){
        sensitiveWordService.save(vo);
        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('im:sensitive:update')")
    public Result<String> update(@RequestBody @Valid ImSensitiveWordVO vo){
        sensitiveWordService.update(vo);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('im:sensitive:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sensitiveWordService.delete(idList);
        return Result.ok();
    }

    @PutMapping("enabled")
    @Operation(summary = "开启或关闭")
    @PreAuthorize("hasAuthority('im:sensitive:update')")
    public Result<String> switchEnabled(@RequestBody @Valid ImSensitiveWordEnabledDTO dto){
        sensitiveWordService.switchEnabled(dto);
        return Result.ok();
    }

}